import string
from array import *
import re
import sys

######################## readSeqsFromFile ##############
def readSeqsFromFile(filename):
    f = open(filename,"r")
    S = f.read()
    f.close()
    Seq = list()
    i = 0
    for a1 in re.finditer("[^ACGT][\n][ACTG\n]+[ACTG][\n]",S,0):
        start = a1.start() + 2
        end = a1.end()
        strl = ""
        for a2 in re.finditer("[ACGT]+[\n]",S[start:end],0):
            i = i + 1
            strl =  strl + a2.string[a2.start():a2.end()-1]
        
        Seq.append(strl)
    
        
    
    return Seq


####################### translateSeq #################
def translateSeq(seq):
    l = len(seq); maxnumbersubstring = int(l/3);
    aminoseq = ""

    subcount = 0;
    for a in re.finditer("[ACGT][ACGT][ACGT]",seq):
        sub = a.string[a.start():a.end()]
        subcount = subcount + 1

   
        c = "-1"
        if sub == "ATT" or sub == "ATC" or sub == "ATA":  c = "I"
        elif sub == "CTT" or sub == "CTC" or sub == "CTA" or sub == "CTG" or sub == "TTA" or sub == "TTG" : c = "L"
        elif sub == "GTT" or sub == "GTC" or sub == "GTA" or sub == "GTG": c = "V"
        elif sub == "TTT" or sub =="TTC": c = "F"
        elif sub == "ATG": c = "M"
        elif sub == "TGT" or sub == "TGC": c = "C"
        elif sub == "GCT" or sub == "GCC" or sub == "GCA" or sub == "GCG": c = "A"
        elif sub == "GGT" or sub == "GGC" or sub == "GGA" or sub == "GGG": c = "G"
        elif sub == "CCT" or sub == "CCC" or sub == "CCA" or sub == "CCG" :  c = "P"
        elif sub == "ACT" or sub == "ACC" or sub == "ACA" or sub == "ACG" :  c = "T"
        elif sub == "TCT" or sub == "TCC" or sub == "TCA" or sub == "TCG" or sub == "AGT" or sub == "AGC": c = "S"
        elif sub == "TAT" or sub == "TAC": c = "Y"
        elif sub == "TGG": c = "W"
        elif sub == "CAA" or sub == "CAG": c = "Q"
        elif sub == "AAT" or sub == "AAC": c = "N"
        elif sub == "CAT" or sub == "CAC": c = "H"
        elif sub == "GAA" or sub == "GAG": c = "E"
        elif sub == "GAT" or sub == "GAC": c = "D"
        elif sub == "AAA" or sub == "AAG": c = "K"
        elif sub == "CGT" or sub == "CGC" or sub == "CGA" or sub == "CGG" or sub == "AGA" or sub == "AGG": c = "R"
        elif sub == "TAA" or sub == "TAG" or sub == "TGA": c = "*"
        
        aminoseq = aminoseq + str(c)
        
    return aminoseq


 #####################################################

def readFindTrans(seq):
    L1 = findFrames(seq)
    revseq = list(seq[::-1])
    for i in range(0,len(revseq)):
        if    revseq[i] == 'A': revseq[i] = 'T'
        elif  revseq[i] == 'T': revseq[i] = 'A'
        elif  revseq[i] == 'C': revseq[i] = 'G'
        elif  revseq[i] == 'G': revseq[i] = 'C'

    revseq = "".join(revseq)
    L2 = findFrames(revseq)
   
    for i in range(len(L1)):
        start = L1[i][0]; stop = (L1[i][0] + L1[i][1] + 3)
        print("Gene " + str(i) + " : Offset: " + str(L1[i][0]) + "  Size: " + str(L1[i][1]))
        print(translateSeq(seq[start:stop]))
        
    l1 = len(L1)
    for j in range(len(L2)):
        start = L2[j][0]; stop = (L2[j][0] + L2[j][1] + 3)
        print("Gene " + str(l1 + j) + " : Offset: " + str(-L2[j][0]) + "  Size: " + str(L2[j][1]))
        print(translateSeq(revseq[start:stop]))
    
    return 


########################################################
def findFrames(seq):
    lseq = len(seq)
    L = list()
    
    laststart = 0
    laststop = 0
    while True:
        a = re.search("ATG(?:[ATGC]{3})*?(TAA|TAG|TGA)",seq[laststart::])
        if(str(a)=="None"): break
        
        start = laststart + a.start()
        stop = laststart + a.end()
        if(stop>laststop and stop-start>=603): 
            L.append(list([start,stop-start - 3]))
            laststop = stop
                
        laststart = start + 1
        
            
    return L

######################################################
######### main script ################################

Seq = readSeqsFromFile(sys.argv[1])
#L = findFrames(str(Seq[0]));
#L = findFrames(Seq[0])





        


